package Entities;

import java.util.ArrayList;
import Entities.Shipping;
import digitshoplogic.DBAccessSingleton;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Carriage_rate {
	public int classId;
	public BigDecimal price;
	public Integer days;

    public Carriage_rate() {
    }

    public Carriage_rate(int classId, BigDecimal price, Integer days) {
        this.classId = classId;
        this.price = price;
        this.days = days;
    }

   public static final Carriage_rate getCarriageRateByID(int rid) throws SQLException {
        Carriage_rate rate = new Carriage_rate();

        PreparedStatement  ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("select * from carriage_rate where carriage_rate.classId = ?");
         ps.setInt(1, rid);

        ResultSet rs = ps.executeQuery();

         if (rs.next()) {
            rate.classId = rs.getInt(1);
            rate.price = rs.getBigDecimal(2);
            rate.days = rs.getInt(3);
        }
        rs.close();
        con.close();
        return rate;
        }

     public static final void addCarriageRateByObj(Carriage_rate rate) throws SQLException {

         PreparedStatement ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("INSERT INTO carriage_rate VALUES (?,?,?)");

         ps.setInt(1, rate.classId);
         ps.setBigDecimal(2, rate.price);
         ps.setInt(3, rate.days);


         ps.executeUpdate();
         ps.close();
         con.close();
        }

 public static final void deleteCarriageRateByID(int id) throws SQLException{

         PreparedStatement  ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("DELETE FROM carriage_rate WHERE carriage_rate.classId= ?");
	 ps.setInt(1, id);
         ps.executeUpdate();
         ps.close();
         con.close();
     }
  public static final void updateCarriageRateByObj(Carriage_rate rate) throws SQLException{

         PreparedStatement  ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("UPDATE carriage_rate SET "
                 + "carriage_rate.price = ?,"
                 + "carriage_rate.days = ? "
                 + " WHERE carriage_rate.classId = ?");

	 ps.setInt(3, rate.classId);
         ps.setBigDecimal(1, rate.price);
         ps.setInt(2, rate.days);

         ps.executeUpdate();
         ps.close();
         con.close();

     }



    public int getClassId() {
        return classId;
    }

    public void setClassId(int classId) {
        this.classId = classId;
    }

    public Integer getDays() {
        return days;
    }

    public void setDays(Integer days) {
        this.days = days;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }   
}